/* 
 * A664p7_internal.h -- A664P7 over Ethernet library functions for internal use only
 *                    See implementation files for documentation
 *
 *------------------------------------------------------------------------------
 *
 *                    ---   PROPRIETARY NOTICE   ---
 *
 *   The information contained herein is proprietary to Smiths Aerospace.
 *   and shall not be reproduced or disclosed in whole or in part or used
 *   for any design or manufacture except when such user possesses direct
 *   written authorization from Smiths Aerospace.
 *
 *   Copyright 2006, Smiths Aerospace.
 *
 *------------------------------------------------------------------------------
 *
 * modification history
 * --------------------
 * 30 mar 06	Huizenga	Initial revision.
 */

#ifndef __INCLUDE_A664P7_INTERNAL_H__
#define __INCLUDE_A664P7_INTERNAL_H__

#include "pcap.h"

#ifndef WIN32
	#include <sys/socket.h>
	#include <netinet/in.h>
#else
	#include <winsock.h>
#endif
#include "protocol.h"
#include "A664p7_api.h"

#define MIN_A664P7_MESSAGE_SIZE 17

/* Ethernet protocol functions */
int eh_to_net(ETHERNET_HEADER *h);
int eh_from_net(ETHERNET_HEADER *h);
int initialize_ethernet_header(ETHERNET_HEADER *h);
int populate_ethernet_header(ETHERNET_HEADER *h, unsigned char *source_mac, unsigned char *dest_mac, unsigned short int type);

/* IP functions */
unsigned short int generate_ip_checksum(IP_HEADER *hdr);
int ih_to_net(IP_HEADER *h);
int ih_from_net(IP_HEADER *h);
int initialize_ip_header(IP_HEADER *h);
int populate_ip_header(IP_HEADER *h, unsigned int src_ip, unsigned int dst_ip);

/* Packet handling functions */
int convert_packet_header_byte_order(PACKET *p, int order);
int setup_packet_header_pointers(PACKET *p);
int setup_packet_pointers(PACKET *p);
int destroy_packet(PACKET *pkt);
PACKET  *create_packet(int max_data_length);
int copy_packet(PACKET *dest, PACKET *source);
int send_A664p7_packet(PACKET *p, pcap_t *device);
PACKET *read_A664p7_packet(pcap_t *device);

/* support functions */
pcap_if_t *get_device_by_number(pcap_if_t *device_list, int d_num);
int show_A664p7_device_list(pcap_if_t *d);
pcap_t *open_A664p7_device(char *dev_name);
void mem_die(void *pointer);
void enable_A664p7_filter(pcap_if_t *sel_dev, pcap_t *device);
void show_mac_address(unsigned char addr[6]);
void show_eth_header(PACKET *p);
char *iptos(u_long in, char *out);
int get_int(char *prompt, int rangel, int rangeh);

/* UDP functions */
void generate_udp_checksum(PACKET *pack, int use_checksum);
void uh_to_net(UDP_HEADER *h);
void uh_from_net(UDP_HEADER *h);
void initialize_udp_header(UDP_HEADER *h);
void populate_udp_header(UDP_HEADER *h, unsigned short int s_port, unsigned short int d_port);

/* VL Handling functions*/
VL_DEVICE_LIST *push_vl_device(VL_DEVICE_LIST *list_top, pcap_t *dev);
VL_LIST *push_vl(VL_LIST *list_top, VL *vl);
void destroy_vl(VL *vl);
int populate_vl(VL *vl, PACKET *pkt, int vl_num, int vl_mode, pcap_t *na_dev, pcap_t *nb_dev);
VL *create_vl(int vl_num, int vl_mode, pcap_t *na_dev, pcap_t *nb_dev, int max_message_size, 
				 unsigned char src_mac[6],
				 unsigned int src_ip, unsigned int dst_ip,
				 unsigned short int src_port, unsigned short int dst_port);
int rx_vl_packet_match(PACKET *p, pcap_t *device, VL *vl);
int count_flag_bits(unsigned int bitfield);
int distribute_packet_to_vls(PACKET *p, pcap_t *device, VL_LIST *vls, int *reject_flags);

void print_vl_flags(int vl_flags);

#endif